// pages/user/find/baskDetail/baskDetail.js
const app = getApp();
//解析html
const WxParse = require('../../../../wxParse/wxParse.js');
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fabulous: false, //点赞状态
    comment: [{}, {}], //发布评论
    commentChild: [], //评论回复
    sunDetails: '', //晒一晒详情
    comments: '', //评论类容
    pid: '',
    focus1: false,
    reply_type: 0,
    seeduid: '',
    baskEaluate: false, //回复显示框
  },

  //点赞
  bindFabulous: function (e) {
    console.log(this.data.sunDetails.is_click);
    if (this.data.sunDetails.is_click == 0) {
      console.log(1);
      this.getClickStatus();
      
    } else {
      console.log(2);
      this.getClick();
      
    }
  },
  //获取评论内容
  getComments: function (e) {
    var that = this;
    let comments = e.detail.value;
    that.setData({
      comments: comments
    })
  },
  //回复评论
  addComment: function (e) {
    var that = this;
    console.log(e, that.data.comment)
    var pid = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    that.setData({
      pid: pid,
      placeholder: '回复：' + name,
      baskEaluate: true
    })
  },
  hideBaskEaluate: function () {
    var that = this;
    that.setData({
      pid: '',
      baskEaluate: false
    })
  },
  stopCommentInput: function () {
    return false;
  },
  //回复评论
  Fromsubmits: function (e) {
    var that = this;
    console.log(that.data)
    var id = e.currentTarget.dataset.id;
    let comments = that.data.comments;
    if (comments == "") {
      wx.showToast({
        title: "请输入评论内容!",
        icon: 'none',
        duration: 2000
      })
      that.setData({
        focus1: true
      });
      return;
    } else {
      that.getReplyComment(e);
      that.setData({
        show: false,
      })
    }

  },
  //发布评论
  Fromsubmit: function (e) {
    var that = this;
    console.log(that.data)
    var id = e.currentTarget.dataset.id;
    let comments = that.data.comments;
    if (comments == "") {
      wx.showToast({
        title: "请输入评论内容!",
        icon: 'none',
        duration: 2000
      })
      that.setData({
        focus1: true
      });
      return;
    } else {
      wx.showToast({
        title: "评论成功",
        icon: 'none',
        duration: 2000
      })
      that.getComment(e)
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: {
        id: options.id,
        comment: 1
      },
    });
    that.getBrowseStatus();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCommentlist();
    this.getNewDetails();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //浏览
  getBrowseStatus: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      aid: that.data.options.id,
      uid: uid.id,
      type: 2
    }
    console.log(param)
    Api.ajax("/app/Zarticle/giveLook", param).then(res => {
      console.log(res)
      if (res.code != 200) {

      }
    })
  },
  //晒一晒详情
  getNewDetails: function () {
    var that = this;
    var uid = wx.getStorageSync('userid');
    var param = {
      aid: that.data.options.id,
      uid: uid.id
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleDetails", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        var article = res.data.content;
        WxParse.wxParse('article', 'html', article, that);
        this.setData({
          sunDetails: res.data
        })
      }
    })
  },

  //点赞 
  getClickStatus: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      aid: that.data.options.id,
      uid: uid.id,
      type: 1
    }
    console.log(param)
    Api.ajax("/app/Zarticle/giveLook", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        setTimeout(function () {
          that.getNewDetails();
        }, 1000);
      }
    })
  },
  //取消点赞 
  getClick: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      aid: that.data.options.id,
      uid: uid.id,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/deleteClick", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        setTimeout(function () {
          that.getNewDetails();
        }, 1000);
      }
    })
  },
  //文章评论列表接口数据
  getCommentlist: function () {
    var that = this;
    var param = {
      aid: that.data.options.id,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/comment_list", param).then(res => {
      console.log(res)
      that.setData({
        comment: res,
      })

    })
  },
  //发布评论
  getComment: function (e) {
    var that = this;
    console.log(that.data)
    let uid = wx.getStorageSync('userid')
    let comments = that.data.comments;
    var param = {
      aid: parseInt(that.data.options.id),
      uid: uid.id,
      content: comments,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/comment", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          seeduid: res.child_list,
          comments: '',
          pid: '',
          baskEaluate: false,
        })
        wx.showToast({
          title: res.msg,
          icon: "success",
          duration: 2000
        });
        setTimeout(function () {
          that.getCommentlist();
        }, 2000)
      }
    })
  },
  //回复评论
  getReplyComment: function (e) {
    var that = this;
    console.log(that.data)
    let uid = wx.getStorageSync('userid')
    let comments = that.data.comments;
    var param = {
      aid: parseInt(that.data.options.id),
      uid: uid.id,
      content: comments,
      pid: parseInt(that.data.pid),
    }
    console.log(param)
    Api.ajax("/app/Zarticle/comment", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          seeduid: res.child_list,
          comments: '',
          pid: '',
          baskEaluate: false,
        })
        wx.showToast({
          title: res.msg,
          icon: "success",
          duration: 2000
        });
        setTimeout(function () {
          that.getCommentlist();
        }, 2000)
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})